const router = require('koa-router')()
const UserHelp =require('../database/users/userHelp')
const MsgHelp =require('../database/msgs/msgHelp')
const GroupHelp =require('../database/groups/GroupHelp')
const socketsHelp =require('../database/sockets/socketsHelp')
const UnReadMsgHelp =require('../database/unread/UnReadMsgHelp')




//测试接口返回时间
router.get('/api/testTime', async (ctx, next) => {

    ctx.body={
        code:0,
        data:'testTime'
    }
})




//查询某个群的用户id
router.post('/api/getGroupUserIds',async (ctx, next) => {

    let gid=ctx.request.body.gid

    let data= await GroupHelp.getGroupUserIds(gid)

    ctx.body={
        code:0,
        data:data
    }
})




//消息是否入库
router.get('/api/test/isMsgsexsit',async (ctx, next) => {

    let {client_time}=ctx.request.query

    let data= await MsgHelp.findMsgByClientTime(client_time)

    ctx.body={
        code:0,
        data:data
    }
})




//获取socketlist
router.get('/api/socketobjs',async (ctx, next) => {

    let data=await socketsHelp.getSocketList()

    ctx.body={
        code:0,
        data:data
    }
})




//获取A发给B的消息数
router.get('/api/getSingleMsgs',async (ctx, next) => {

    let {from_uid,to_uid}=ctx.request.query
    let  data=await MsgHelp.getMsgList(to_uid,from_uid)

    ctx.body={
        code:0,
        data:data
    }
})




//获取某群有多少条消息
router.get('/api/getGroupMsgsLength',async (ctx, next) => {

    let {gid}=ctx.request.query
    let  data=await MsgHelp.getGroupMsgListLength(gid-0)

    ctx.body={
        code:0,
        data:data
    }
})



//获取某个群所有的消息
router.get('/api/getGroupMsgs',async (ctx, next) => {

    let {from_uid,gid}=ctx.request.query
    let  data=await MsgHelp.getGroupMsgList(gid,from_uid)

    ctx.body={
        code:0,
        data:data
    }
})




//查看pushlastmsg接口返回的数据
router.get('/api/test/pushLastMsg',async (ctx, next) => {

    let {userid}=ctx.request.query
    let  lastmsg=await MsgHelp.pushLastMsg(userid)

    ctx.body={
        code:0,
        data:lastmsg
    }
})




//获取某个群的详细信息
router.get('/api/test/getGroupInfo',async (ctx, next) => {
    let {gid}=ctx.request.query
    let data=await GroupHelp.getGroupInfo(gid)
    ctx.body={
        code:0,
        data:data
    }

})




//获取某个人的未读消息
router.get('/api/test/unreadMsg',async (ctx, next) => {
    let {to_uid}=ctx.request.query
    let data=await UnReadMsgHelp.getUnReadMsg(to_uid)
    ctx.body={
        code:0,
        data:data
    }

})





//查看pushlastmsgforme接口返回的数据
router.get('/api/test/pushlastforme',async (ctx, next) => {
    let {to_uid,myuid}=ctx.request.query


    let msgs=await MsgHelp.getMsgList(to_uid-0,myuid-0)

    let lastmsg=msgs[msgs.length-1]

     let  lastmsg_=await MsgHelp.pushLastMsgForMe(to_uid-0,myuid-0,lastmsg)

    ctx.body={
        code:0,
        data:lastmsg_
    }
})




//socketList，已停，影响性能
router.get('/api/test/getSocketObjsNum',async (ctx, next) => {

    let num=await socketsHelp.getSocketListNum()


    ctx.body={
        code:0,
        data:{
            socket_num:num,
            // status:'此表用来辅助测试,监控用户的连接情况,当前状态已关闭。不影响业务，因为在实际业务代码中，用户的连接状态是写在内存中的',
            // msg:'此表为每个用户在登录时建立连接，用户退出登录，掉线，退到后台等操作，都需要对该表进行读写操作，属于大量数据的高并发'
        }
    }
})




//获取用户总数
router.get('/api/test/getUserNum',async (ctx, next) => {

    let lists=await UserHelp.getUserList()


    ctx.body={
        code:0,
        data:{
            user_num:lists.length
        }
    }
})




//获取某个用户
router.get('/api/test/getUserByUid',async (ctx, next) => {

    let {uid}=ctx.request.query
    let lists=await UserHelp.getUserListTestByuid(uid-0)

    ctx.body={
        code:0,
        data:lists
    }
})




//查看总共有多少群
router.get('/api/test/getGroupNum',async (ctx, next) => {

    let num=await GroupHelp.getGroupNum()


    ctx.body={
        code:0,
        data:{
            num:num
        }
    }
})



//查看总共有多少消息
router.get('/api/test/allmsgsnum',async (ctx, next) => {


    let data= await MsgHelp.findAllMsg()

    ctx.body={
        code:0,
        data:data
    }
})





//统计华人街聊天消息：总人数，总群数，总消息数， 返回一个页面

router.get('/api/test/ejs',async (ctx, next) => {

    let msg_num= await MsgHelp.findAllMsg()
    let group_num=await GroupHelp.getGroupNum()
    let user_lists=await UserHelp.getUserList()

    await ctx.render('index', {
        msg_num,
        group_num,
        user_num:user_lists.length
    })

})






module.exports = router